﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using LiuYan.Entity;
using LiuYan.Service;
using LiuYan.Web.Areas.Admin.Controllers;
using LiuYan.Interface;

namespace LiuYan.Web.Areas.Admin.Controllers
{
    public class WxMenuController : BaseController
    {
        //
        // GET: /Admin/Menu/


        public ActionResult Window()
        {
            ViewData["msglist"] = new ImgArticleService().GetList();
            ViewData["pmenu"] = new WxMenuService().GetPMenu();
            return View();
        }

        public string Edit(FormCollection form)
        {
            WxMenuService menuService = new WxMenuService();
            var id = Convert.ToInt32(form["id"]);
            var menuId = Convert.ToInt32(form["pmenu"]);
            var url = form["url"];
            var shijian = form["event"];
            var menuName = form["text"];
            var order = Convert.ToInt32(form["order"]);
            TB_ChildMenu centity=new TB_ChildMenu();
            TB_ParentMenu pentity=new TB_ParentMenu();
            if (menuId == 0)
            {
                pentity.Id = id;
                pentity.MenuName = menuName;
                pentity.Type = url != "" ? "view" : "click";
                pentity.Key = url != "" ? url : shijian;
                pentity.Order = order;
                if (menuService.GetPMenuCount() >= 3 &&id==0)
                {
                    return "p_3";
                }
                else if (id != 0)
                {
                    if (menuService.ExsitFMenu(id) == 0)
                    {
                        if (menuService.GetPMenuCount() == 3)
                        {
                            return "p_3";
                        }
                    }
                    menuService.EditP(pentity);
                }
                else
                {
                    menuService.EditP(pentity);
                }
                }
           if (menuId > 0)
            {
                centity.Id = id;
                centity.MenuName = menuName;
                centity.Pid = menuId;
                centity.Type = url != "" ? "view" : "click";
                centity.Key = url != "" ? url : shijian;
                centity.Order = order;
                if (menuService.GetCMenuCount(menuId) >= 5 && id == 0)
                {
                    return "c_5";
                }
                else if (id != 0)
                {
                    if (menuService.ExsitCMenu(id,menuId) == 0)
                    {
                        if (menuService.GetPMenuCount() == 6)
                        {
                            return "c_5";
                        }
                    }
                    menuService.EditC(centity);
                }
                else
                {
                    
                    menuService.EditC(centity);
                }
            }
            return "c_1";
        }

        public string GetList()
        {
            return JsonConvert.SerializeObject(new WxMenuService().GetList());
        }

        public int Del(string id,string menu)
        {
            return new WxMenuService().DelMenu(id, menu);
        }

        public string GetOneData(string id, string menu)
        {
          menu=menu=="c"?"TB_ChildMenu":"TB_ParentMenu";
            return JsonConvert.SerializeObject(new BaseService().GetOneData(Convert.ToInt32(id), menu));
        }

        public string PublishMenu()
        {
            WxMenu wxMenu=new WxMenu();
            
            var tablep = new WxMenuService().GetPMenu();
            string menu = "{" +
                          "\"button\": [";
            foreach (DataRow row in tablep.Rows)
            {
                var tablec = new WxMenuService().GetCMenu(Convert.ToInt32(row["Id"]));
                if (tablec.Rows.Count == 0)
                {
                    menu += "{" +
                           "\"type\": \"" + row["Type"] + "\", " +
                           "\"name\": \"" + row["MenuName"] + "\", " +
                           "\"" + (row["Type"].ToString()=="view"?"url":"key") + "\": \"" + row["Key"] + "\"" +
                           "}, ";
                }
                else
                {
                    menu +=
                        "{" +
                        "\"name\": \"" + row["MenuName"] + "\", " +
                        "\"sub_button\": [";
                    foreach (DataRow row1 in tablec.Rows)
                    {
                        menu +=
                            "{" +
                            "\"type\": \""+row1["Type"]+"\", " +
                            "\"name\": \""+row1["MenuName"]+"\", " +
                            "\"" + (row1["Type"].ToString() == "view" ? "url" : "key") + "\": \"" + row1["Key"] + "\"" +
                            "},";
                    }
                    menu = menu.Substring(0, menu.Length - 1);
                    menu += "]},";
                }
            }
            menu = menu.Substring(0, menu.Length - 1);
                    menu+="]}";
            var f = "ok";
            try
            {
                var token = new WxAccess_Token().GetAccessToken();
                wxMenu.DelMenu(token);
                f = wxMenu.CreatMenu(menu, token);

            }
            catch (Exception e)
            {
                return "error";
            }


            return f;
        }
    }
}
